<template>
    <div class="ve_container">
        <!-- 搜索 -->
        <el-form ref="queryForm" :inline="true" :model="params">
            <el-form-item label="描述" prop="desc">
                <el-input
                    v-model="desc"
                    placeholder="请输入描述"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item label="唯一性编码" prop="code">
                <el-input
                    v-model="code"
                    placeholder="请输入唯一性编码"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item label="创建时间" prop="createTime">
                <el-input
                    v-model="createTime"
                    placeholder="请输入创建时间"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item label="dockerfile文件" prop="dockerfile">
                <el-input
                    v-model="dockerfile"
                    placeholder="请输入dockerfile文件"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item label="主键ID" prop="id">
                <el-input
                    v-model="id"
                    placeholder="请输入主键ID"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item label="是否删除" prop="isDeleted">
                <el-input
                    v-model="isDeleted"
                    placeholder="请输入是否删除"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item label="仓库ID" prop="repositoryId">
                <el-input
                    v-model="repositoryId"
                    placeholder="请输入仓库ID"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item label="应用类型（Java、python、vue）" prop="type">
                <el-input
                    v-model="type"
                    placeholder="请输入应用类型（Java、python、vue）"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item label="更新时间" prop="updateTime">
                <el-input
                    v-model="updateTime"
                    placeholder="请输入更新时间"
                    clearable
                ></el-input>
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    @click="onSubmit(params, getDataList)"
                >
                    {{ buttons.search.name }}
                </el-button>
                <el-button @click="resetForm(queryForm, params, getDataList)">
                    重置
                </el-button>
            </el-form-item>
        </el-form>

        <!-- 列表 -->
        <ve-table
            :table="{
                data: tableData,
            }"
            :pagination="{
                onSizeChange: (val) =>
                    handleSizeChange(val, params, getDataList),
                onCurrentChange: (val) =>
                    handleCurrentChange(val, params, getDataList),
                currentPage: current,
                pageSize: size,
                total: total,
            }"
        >
            <template #tool_bar>
                <el-button
                    v-permission="['add']"
                    size="small"
                    type="primary"
                    @click="handleEdit(buttons.add.name)"
                >
                    {{ buttons.add.name }}
                </el-button>

                <el-button
                    v-permission="['downLoadExcel']"
                    size="small"
                    type="primary"
                    @click="downLoadExcel()"
                >
                    {{ buttons.downLoadExcel.name }}
                </el-button>
                <el-button
                    v-permission="['downLoadExportTemplateExcel']"
                    size="small"
                    type="primary"
                    @click="downLoadExportTemplateExcel()"
                >
                    {{ buttons.downLoadExportTemplateExcel.name }}
                </el-button>
                <el-button
                    v-permission="['importExcel']"
                    size="small"
                    type="primary"
                    @click="handleUpload(buttons.importExcel.name)"
                >
                    {{ buttons.importExcel.name }}
                </el-button>
            </template>

            <el-table-column prop="desc" label="描述"></el-table-column>

            <el-table-column prop="code" label="唯一性编码"></el-table-column>

            <el-table-column
                prop="createTime"
                label="创建时间"
            ></el-table-column>

            <el-table-column
                prop="dockerfile"
                label="dockerfile文件"
            ></el-table-column>

            <el-table-column prop="id" label="主键ID"></el-table-column>

            <el-table-column
                prop="isDeleted"
                label="是否删除"
            ></el-table-column>

            <el-table-column
                prop="repositoryId"
                label="仓库ID"
            ></el-table-column>

            <el-table-column
                prop="type"
                label="应用类型（Java、python、vue）"
            ></el-table-column>

            <el-table-column
                prop="updateTime"
                label="更新时间"
            ></el-table-column>

            <!--            <el-table-column prop="username" label="用户名"> </el-table-column>-->
            <el-table-column fixed="right" label="操作">
                <template v-slot:default="{ row }">
                    <el-button
                        v-permission="['edit']"
                        @click.prevent="handleEdit(buttons.edit.name, row)"
                        type="primary"
                        size="small"
                    >
                        {{ buttons.edit.name }}
                    </el-button>
                    <el-button
                        v-permission="['del']"
                        @click.prevent="handleDel(row.id)"
                        type="danger"
                        size="small"
                    >
                        {{ buttons.del.name }}
                    </el-button>
                </template>
            </el-table-column>
        </ve-table>

        <!-- 编辑组件 -->
        <DockerBuilderEdit
            v-if="showDialog"
            :rowData="rowData"
            :title="dialogTitle"
            :showDialog="showDialog"
            @closeDialog="closeEditDialog($event)"
        />
        <!-- 上传组件 -->
        <DockerBuilderUpload
            v-if="showUploadDialog"
            :title="dialogTitle"
            :showDialog="showUploadDialog"
            @closeDialog="closeUploadDialog($event)"
        />
    </div>
</template>
<script>
import menu from "./components/menu";
import { moduleName } from "@/config";

export default {
    data: () => ({
        description: "打包管理",
        buttons: {
            search: { name: "查询" },
            add: { name: "添加" },
            edit: { name: "编辑" },
            del: { name: "删除" },
            downLoadExcel: { name: "导出结果" },
            downLoadExportTemplateExcel: { name: "导出模版" },
            importExcel: { name: "导入" },
        },
        // type 0:目录 1：菜单 2：按钮
        type: "1",
        icon: "Stamp",
        name: "打包管理",
        parentMenu: menu,
        module: moduleName,
    }),
};
</script>

<script setup>
import DockerBuilderEdit from "./components/DockerBuilderEdit.vue";
import DockerBuilderUpload from "./components/DockerBuilderUpload.vue";
import { reactive, toRefs, ref, onMounted, getCurrentInstance } from "vue";

//*导入公共查询方法
import {
    onSubmit,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
} from "@/views/pages/common";

const { proxy } = getCurrentInstance();

const rowData = ref(null);
const dialogTitle = ref("");
const showDialog = ref(false);
const showUploadDialog = ref(false);

const queryForm = ref(null);
const tableData = ref([]);
const params = reactive({
    desc: "",
    code: "",
    createTime: "",
    dockerfile: "",
    id: "",
    isDeleted: "",
    repositoryId: "",
    type: "",
    updateTime: "",
    size: 10,
    current: 1,
    total: 0,
});
const {
    desc,
    code,
    createTime,
    dockerfile,
    id,
    isDeleted,
    repositoryId,
    type,
    updateTime,
    size,
    current,
    total,
} = toRefs(params);

/**
 * @description:添加or编辑事件
 * @return {*}
 * @param title
 * @param row
 */
const handleEdit = (title, row = null) => {
    showDialog.value = true;
    dialogTitle.value = title;
    rowData.value = row;
};
/**
 * @description: dialog事件
 * @return {*}
 * @param e
 */
const closeEditDialog = (e) => {
    showDialog.value = e;
    getDataList();
};

/**
 * @description: 上传Excel文件
 * @return {*}
 * @param title
 * @param row
 */
const handleUpload = (title, row = null) => {
    showUploadDialog.value = true;
    dialogTitle.value = title;
    rowData.value = row;
};
/**
 * @description: dialog事件
 * @return {*}
 * @param e
 */
const closeUploadDialog = (e) => {
    showUploadDialog.value = e;
    getDataList();
};
/**删除行数据
 * @description:
 * @param {*}
 * @return {*}
 */
const handleDel = (id) => {
    proxy
        .$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "error",
        })
        .then(async () => {
            const { code } = await VE_API.DockerBuilderApi.dockerBuilderRemove({
                id,
            });
            if (code === 0) {
                getDataList();
            }
        })
        .catch(() => {
            proxy.$message({
                type: "info",
                message: "已取消删除",
            });
        });
};

/**
 * 文件模版下载
 */
const downLoadExportTemplateExcel = async () => {
    queryForm.value.validate(async (valid) => {
        if (valid) {
            let res =
                await VE_API.DockerBuilderApi.dockerBuilderExportTemplateExcel(
                    params,
                    {
                        responseType: "blob",
                    },
                );
            let fileName = res.headers["file-name"];
            // 获取文件名
            fileName = decodeURIComponent(fileName);
            let url = window.URL.createObjectURL(new Blob([res.data]));
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            // eslint-disable-next-line no-undef
            link.setAttribute("download", fileName); //指定下载后的文件名，防跳转
            document.body.appendChild(link);
            link.click();
            // 释放内存
            window.URL.revokeObjectURL(link.href);
        } else {
            return false;
        }
    });
};

/**
 * 文件下载
 */
const downLoadExcel = async () => {
    queryForm.value.validate(async (valid) => {
        if (valid) {
            let res = await VE_API.DockerBuilderApi.dockerBuilderExportExcel(
                params,
                {
                    responseType: "blob",
                },
            );
            let fileName = res.headers["file-name"];
            // 获取文件名
            fileName = decodeURIComponent(fileName);
            let url = window.URL.createObjectURL(new Blob([res.data]));
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            // eslint-disable-next-line no-undef
            link.setAttribute("download", fileName); //指定下载后的文件名，防跳转
            document.body.appendChild(link);
            link.click();
            // 释放内存
            window.URL.revokeObjectURL(link.href);
        } else {
            return false;
        }
    });
};
/**
 * @description: 获取列表数据
 * @param {*}
 * @return {*}
 */
const getDataList = async () => {
    const { code, data } =
        await VE_API.DockerBuilderApi.dockerBuilderFindPage(params);
    if (code === 0) {
        const { size, current, total, records } = data;
        params.size = size;
        params.current = current;
        params.total = total;
        tableData.value = records;
    }
};
onMounted(async () => {
    await getDataList();
});
</script>

<style lang="scss" scoped></style>
